package com.payment.ktb.activity.main1;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.activity.MainActivity;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.LogUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by admin on 2017/9/9.
 */

public class OrderConfirmWaitActivity extends BaseActivity {




    @BindView(R.id.tv_orderconfirmwait_amount)
    TextView tv_orderconfirmwait_amount;
    @BindView(R.id.tv_orderconfirmwait_goodsname)
    TextView tv_orderconfirmwait_goodsname;
    @BindView(R.id.tv_orderconfirmwait_bankname)
    TextView tv_orderconfirmwait_bankname;
    @BindView(R.id.tv_orderconfirmwait_bankno)
    TextView tv_orderconfirmwait_bankno;
    @BindView(R.id.tv_orderconfirmwait_orderno)
    TextView tv_orderconfirmwait_orderno;
    @BindView(R.id.tv_orderconfirmwait_ordertime)
    TextView tv_orderconfirmwait_ordertime;

    boolean queryFlag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_orderconfirmwait);
        ButterKnife.bind(this);
        hideTitleBack();
        initTitle("订单确认");
        initData();
        getOrderStatus();
    }

    private void initData() {
        tv_orderconfirmwait_amount.setText("¥ " + handleFen2Yuan(getIntent().getStringExtra("amount")));
        tv_orderconfirmwait_goodsname.setText(getIntent().getStringExtra("goodsName"));
        if (!TextUtils.isEmpty(getIntent().getStringExtra("goodsName")) && "Vip1".equals(getIntent().getStringExtra("goodsName"))) {
            tv_orderconfirmwait_goodsname.setText("会员购买");
        }else if (!TextUtils.isEmpty(getIntent().getStringExtra("goodsName")) && "Vip2".equals(getIntent().getStringExtra("goodsName"))) {
            tv_orderconfirmwait_goodsname.setText("会员购买");
        }else if (!TextUtils.isEmpty(getIntent().getStringExtra("goodsName")) && "151".equals(getIntent().getStringExtra("goodsName"))) {
            tv_orderconfirmwait_goodsname.setText("后台管理费");
        }
        tv_orderconfirmwait_bankname.setText(getIntent().getStringExtra("cardBank"));
        tv_orderconfirmwait_bankno.setText(getIntent().getStringExtra("cardNo"));
        tv_orderconfirmwait_orderno.setText(getIntent().getStringExtra("orderNo"));
        tv_orderconfirmwait_ordertime.setText(getIntent().getStringExtra("orderTime"));
    }

    private String handleFen2Yuan(String fen) {
        if (fen.equals("0")) {
            return "0.00";
        }
        double srcFen = Double.valueOf(fen);
        double roundSrcFen = Math.round(srcFen);
        return String.valueOf(roundSrcFen/100);
    }

    @OnClick({R.id.btn_orderconfirmwait_confirm})
    public void clickEvent(View view) {

        switch (view.getId()) {


            //确定按钮
            case R.id.btn_orderconfirmwait_confirm:
                startActivity(MainActivity.class);
                break;

        }


    }







    //查询订单状态
    public void getOrderStatus() {


        //构造传输参数
        final Map<String, String> map = new HashMap<String,String>();
        map.put("linkId",String.valueOf(new Date().getTime()));
        map.put("orderNo",getIntent().getStringExtra("orderNo"));

        //向服务器发送请求
        HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.PayOrderStatus, map, mQueue, new HttpCallbackListener() {
            @Override
            public void onFinish(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);

                    Intent intent = null;

                    if (jsonObject.optString("orderStatus").equals("20") || jsonObject.optString("orderStatus").equals("21")) {
                        if (jsonObject.optString("orderStatus").equals("20") ) {
                            intent = new Intent(mContext,OrderConfirmSuccessActivity.class);
                        }else {
                            intent = new Intent(mContext,OrderConfirmFailActivity.class);
                        }
                        intent.putExtra("amount",getIntent().getStringExtra("amount"));

                        if ("1000".equals(jsonObject.optString("product"))) {
                            intent.putExtra("goodsName","商户收款");
                        }else if ("1001".equals(jsonObject.optString("product"))) {
                            intent.putExtra("goodsName","会员购买");
                        }else if ("1002".equals(jsonObject.optString("product"))) {
                            intent.putExtra("goodsName","后台管理费");
                        }else {
                            intent.putExtra("goodsName","商户收款");
                        }

                        intent.putExtra("cardBank",getIntent().getStringExtra("cardBank"));
                        intent.putExtra("cardNo",getIntent().getStringExtra("cardNo"));
                        intent.putExtra("orderNo",jsonObject.optString("orderNo"));
                        intent.putExtra("orderTime",jsonObject.optString("orderTime"));
                        intent.putExtra("ordermemo",jsonObject.optString("ordermemo"));
                        startActivity(intent);

                    }else if (queryFlag){
                        getOrderStatus();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                    AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                }
            }

            @Override
            public void onError(VolleyError volleyError) {
                AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
            }
        });

    }


    @Override
    protected void onPause() {
        super.onPause();
        queryFlag = false;
    }
}
